package cn.schoolwow.quickdao.test.module.business.user.ddl.flow.column;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.database.parent.DatabaseType;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.domain.entity.PropertyOption;
import cn.schoolwow.quickdao.domain.transaction.Transaction;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class CreateNumberColumnFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        DAO dao = flowContext.checkInstanceData(DAO.class);
        DatabaseType databaseType = flowContext.checkInstanceData(DatabaseType.class);

        Entity userEntity = dao.getEntity(User.class);

        PropertyOption numberPropertyOption = new PropertyOption();
        numberPropertyOption.column = "number";
        numberPropertyOption.columnType = "int4";
        numberPropertyOption.comment = "工号";
        switch (databaseType){
            case SQLite:{
                numberPropertyOption.notNull = false;
            }break;
            default:{
                numberPropertyOption.notNull = true;
            }
        }
        flowContext.putTemporaryData("numberPropertyOption", numberPropertyOption);

        if(flowContext.containInstanceKey(Transaction.class)){
            Transaction transaction = flowContext.checkInstanceData(Transaction.class);
            transaction.createColumn(userEntity.tableName, numberPropertyOption);
        }else{
            dao.createColumn(userEntity.tableName, numberPropertyOption);
            Property numberProperty = dao.getProperty(userEntity.tableName, numberPropertyOption.column);
            flowContext.putTemporaryData("numberProperty" ,numberProperty);
        }
    }

    @Override
    public String name() {
        return "创建工号字段";
    }
}
